package com.jacobson.huobi.mvp.adapters

import android.content.Context
import android.graphics.Color
import android.text.Html
import android.text.format.DateFormat
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.caverock.androidsvg.SVGImageView
import com.jacobson.core.di.annotation.ActivityScope
import com.jacobson.core.widget.adapter.ListRecyclerAdapter
import com.jacobson.huobi.Constants
import com.jacobson.huobi.R
import com.jacobson.huobi.common.constants.*
import com.jacobson.huobi.components.CommonListRecyclerAdapter
import com.jacobson.huobi.entities.AlarmInfo
import com.jacobson.huobi.entities.PriceStatusColorEnum
import com.jacobson.huobi.entities.SymbolInfo
import com.jacobson.huobi.mvp.AlarmRecordActivity
import com.jacobson.huobi.utils.BitmapUtil
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.item_symbol.view.*
import java.text.DecimalFormat
import java.util.*
import javax.inject.Inject
import javax.inject.Named


@ActivityScope
class SymbolAdapter @Inject constructor(@Named("context") context: Context) :
    CommonListRecyclerAdapter<SymbolInfo>(context) {
    @field:Transient
    private var twinkling: Boolean = false
    @field:Transient
    private var expandPosition: Int = -1

    companion object {
        // 0-初始状态, 1-成功, 2-未知错误, 3-K线获取错误，4-指标计算错误，5-策略计算错误, 6-策略系统记录缺失
        private val STRATEGY_ERR_TIPS = arrayOf("初始", "", "未知", "K线", "指标", "策略", "记录")
        // 0-无, 1-3线联动, 2-4线联动, 3-3线空, 4-4线空, 5-顶部震荡, 6-底部震荡, 7-顶中阴, 8-底中阴, F-操盘线计算出错
        private val TRADING_LINE_TIPS =
            arrayOf(
                "", "3线联动", "4线联动", "3线空", "4线空", "顶部震荡", "底部震荡", "顶中阴", "底中阴",
                "", "", "", "", "", "", "", "出错"
            )
    }
    override fun onCreateContentViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ContentViewHolder<SymbolInfo> =
        ViewHolder(context, parent)

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as? ViewHolder)?.setExpendPosition(this.expandPosition)
        super.onBindViewHolder(holder, position)
    }

    fun setTwinkling(twinkling: Boolean) {
        if (this.twinkling != twinkling) {
            this.twinkling = twinkling
            notifyItemRangeChanged(0, getCustomItemCount(), twinkling)
        }
    }

    fun setExpandPosition(position: Int) {
        if (this.expandPosition == position) {
            this.expandPosition = -1
        } else {
            this.expandPosition = position
        }
        notifyDataSetChanged()
    }

    class ViewHolder(
        context: Context?,
        parent: ViewGroup
    ) : ListRecyclerAdapter.ContentViewHolder<SymbolInfo>(context, parent, R.layout.item_symbol) {

        private var expendPosition: Int = -1

        fun setExpendPosition(position: Int) {
            this.expendPosition = position
        }

        override fun onBindView(items: List<SymbolInfo>, item: SymbolInfo, position: Int) {
            val symbolName = item.currency + item.transCurrency
            val transCurrency = if (symbolName.length > 6) {
                item.transCurrency.substring(0, symbolName.length - 6)
            } else {
                item.transCurrency
            }
            itemView.tvSymbolName.text = Html.fromHtml(
                ResourcesUtil.formatString(
                    R.string.fm_symbol_name,
                    item.currency,
                    transCurrency
                ).toUpperCase(Locale.getDefault())
            )
            itemView.tvSymbolCurPrice.text = item.realPrice.ignoreEndZero()
            with(itemView.tvSymbolChanged) {
                text = ResourcesUtil.formatString(R.string.fm_symbol_percent, item.changed)
                setBackgroundResource(if (item.changed > 0) R.color.bg_rise else R.color.bg_down)
            }

//            itemView.tvSymbolProfitability.text =
//                ResourcesUtil.formatString(R.string.fm_symbol_percent, item.profitability)
            // 交易量显示到百万
            val volume = item.volume / Constants.SYMBOL_VOLUME_UNIT
            if (volume > 100) {
                itemView.tvSymbolVolume.text =
                    ResourcesUtil.formatString(R.string.fm_symbol_double_int, volume)
            } else {
                itemView.tvSymbolVolume.text =
                    ResourcesUtil.formatString(R.string.fm_symbol_double_one, volume)

            }
            with(item.alarmH1) {
                itemView.tvSymbolBaseInfoH1.text = ResourcesUtil.formatString(
                    R.string.fm_symbol_base_info,
                    formatPeriod(),
                    formatType(context)
                )

                if (item.realtimePrimary) {
                    itemView.tvSymbolAlarmTimeH1.text = this.formatAlarmTime()
                    itemView.tvSymbolPriceH1.text = this.formatAlarmPrice()
                } else {
                    itemView.tvSymbolAlarmTimeH1.text = "game over"
                    itemView.tvSymbolPriceH1.text = "-1"
                }
                if (AlarmTypeConstant.isAlarmTypeStart(this.type)) {
                    itemView.tvSymbolPriceH1.setTextColor(Color.parseColor("#000000"))
                    itemView.tvSymbolPriceH1.setBackgroundColor(
                        Color.parseColor(
                            PriceStatusColorEnum.ALARMH1_START2.color
                        )
                    )
                } else {
                    itemView.tvSymbolPriceH1.setTextColor(getTextColorByStatus(1, this))
                    itemView.tvSymbolPriceH1.setBackgroundColor(Color.TRANSPARENT)
                }
//                tvSymbolRLH1
                itemView.tvSymbolAlarmProfitabilityH1.text =
                    this.formatAlarmProfitability() + "/" + this.formatAlarmProfitabilityCumulative()

                if (LinkStatusConstant.isLowLinkHigh(item.alarmH1.extraSign)) {
//                    itemView.tvSymbolAlarmLinkH1H4Right.visibility = View.VISIBLE
                    itemView.tvSymbolAlarmLinkH1H4Right.text = this.getLowLinkHigh()
                } else {
                    itemView.tvSymbolAlarmLinkH1H4Right.text = " "
                }
                if (LinkStatusConstant.isHighLinkLow(item.alarmH1.extraSign)) {
//                    itemView.tvSymbolAlarmLinkH1H4Left.visibility = View.VISIBLE
                    itemView.tvSymbolAlarmLinkH1H4Left.text = this.getHighLinkLow()
                } else {
                    itemView.tvSymbolAlarmLinkH1H4Left.text = " "
                }
                showTradingLineStatus(itemView.tvSymbolAlarmLinkH1H4, item.alarmH1)
                errorCodeText(itemView.tvSymbolBaseInfoCode, item.alarmH1)
                itemView.tvLinkStatusAndKLineNumH1.text =
                    this.getLinkStatusAndKLineNum(PeriodOffsetConstant.TYPE_MIN60)


            }
            with(item.alarmH4) {
                itemView.tvSymbolPriceH4.text = this.formatAlarmPrice()

                itemView.tvSymbolBaseInfoH4.text = ResourcesUtil.formatString(
                    R.string.fm_symbol_base_info,
                    formatPeriod(),
                    formatType(context)
                )
                itemView.tvSymbolAlarmTimeH4.text = this.formatAlarmTime()
                itemView.tvSymbolAlarmProfitabilityH4.text =
                    this.formatAlarmProfitability() + "/" + this.formatAlarmProfitabilityCumulative()

                if (AlarmTypeConstant.isAlarmTypeStart(this.type)) {
                    itemView.tvSymbolPriceH4.setTextColor(Color.parseColor("#000000"))
                    itemView.tvSymbolPriceH4.setBackgroundColor(
                        Color.parseColor(
                            PriceStatusColorEnum.ALARMH1_START2.color
                        )
                    )
                } else {
                    itemView.tvSymbolPriceH4.setTextColor(getTextColorByStatus(1, this))
                    itemView.tvSymbolPriceH4.setBackgroundColor(Color.TRANSPARENT)
                }

                if (LinkStatusConstant.isLowLinkHigh(item.alarmH4.extraSign)) {
//                    itemView.tvSymbolAlarmLinkH4H12Right.visibility = View.VISIBLE
                    itemView.tvSymbolAlarmLinkH4H12Right.text = this.getLowLinkHigh()
                } else {
                    itemView.tvSymbolAlarmLinkH4H12Right.text = " "
                }
                if (LinkStatusConstant.isHighLinkLow(item.alarmH4.extraSign)) {
//                    itemView.tvSymbolAlarmLinkH4H12Left.visibility = View.VISIBLE
                    itemView.tvSymbolAlarmLinkH4H12Left.text = this.getHighLinkLow()
                } else {
                    itemView.tvSymbolAlarmLinkH4H12Left.text = " "
                }
                showTradingLineStatus(itemView.tvSymbolAlarmHLink, item.alarmH4)
                itemView.tvSymbolAlarmH4LevelAndKNum.text =
                    getAlarmLevelAndKNum(item.prioritySetH4, item.priorityNumH4)
                itemView.tvLinkStatusAndKLineNumH4.text =
                    this.getLinkStatusAndKLineNum(PeriodOffsetConstant.TYPE_MIN60)
                errorCodeText(itemView.tvSymbolBaseInfoH4Code, item.alarmH4)

            }
            with(item.alarmH12) {
                itemView.tvSymbolPriceH12.text = this.formatAlarmPrice()
                itemView.tvSymbolBaseInfoH12.text = ResourcesUtil.formatString(
                    R.string.fm_symbol_base_info,
                    formatPeriod(),
                    formatType(context)
                )
                if (AlarmTypeConstant.isAlarmTypeStart(this.type)) {
                    itemView.tvSymbolPriceH12.setTextColor(Color.parseColor("#000000"))
                    itemView.tvSymbolPriceH12.setBackgroundColor(
                        Color.parseColor(
                            PriceStatusColorEnum.ALARMH1_START2.color
                        )
                    )
                } else {
                    itemView.tvSymbolPriceH12.setTextColor(getTextColorByStatus(1, this))
                    itemView.tvSymbolPriceH12.setBackgroundColor(Color.TRANSPARENT)
                }
//                itemView.tvSymbolBaseInfoH12.setTextColor(getTextColorByStatus(1, item.alarmH12))
                itemView.tvSymbolAlarmTimeH12.text = this.formatAlarmTime()
                itemView.tvSymbolAlarmH12LevelAndKNum.text =
                    getAlarmLevelAndKNum(item.prioritySetH12, item.priorityNumH12)
                itemView.tvSymbolAlarmProfitabilityH12.text =
                    this.formatAlarmProfitability() + "/" + this.formatAlarmProfitabilityCumulative()

                if (LinkStatusConstant.isLowLinkHigh(item.alarmH12.extraSign)) {
//                    itemView.tvSymbolAlarmLinkH12DayRight.visibility = View.VISIBLE
                    itemView.tvSymbolAlarmLinkH12DayRight.text = this.getLowLinkHigh()
                } else {
                    itemView.tvSymbolAlarmLinkH12DayRight.text = " "
                }
                if (LinkStatusConstant.isHighLinkLow(item.alarmH12.extraSign)) {
//                    itemView.tvSymbolAlarmLinkDayH12Left.visibility = View.VISIBLE
                    itemView.tvSymbolAlarmLinkDayH12Left.text = this.getHighLinkLow()
                } else {
                    itemView.tvSymbolAlarmLinkDayH12Left.text = " "
                }
                showTradingLineStatus(itemView.tvSymbolAlarmH12Link, item.alarmH12)
                itemView.tvLinkStatusAndKLineNumH12.text =
                    this.getLinkStatusAndKLineNum(PeriodOffsetConstant.TYPE_MIN60)
                errorCodeText(itemView.tvSymbolBaseInfoHCode, item.alarmH12)

            }
            with(item.alarmDay) {
                itemView.tvSymbolPriceDay.text = this.formatAlarmPrice()
                itemView.tvSymbolBaseInfoDay.text = ResourcesUtil.formatString(
                    R.string.fm_symbol_base_info,
                    formatPeriod(),
                    formatType(context)
                )
                if (AlarmTypeConstant.isAlarmTypeStart(this.type)) {
                    itemView.tvSymbolPriceDay.setTextColor(Color.parseColor("#000000"))
                    itemView.tvSymbolPriceDay.setBackgroundColor(
                        Color.parseColor(
                            PriceStatusColorEnum.ALARMH1_START2.color
                        )
                    )
                } else {
                    itemView.tvSymbolPriceDay.setTextColor(getTextColorByStatus(1, this))
                    itemView.tvSymbolPriceDay.setBackgroundColor(Color.TRANSPARENT)
                }
//                itemView.tvSymbolBaseInfoDay.setTextColor(getTextColorByStatus(1, item.alarmDay))
                itemView.tvSymbolAlarmTimeDay.text = this.formatAlarmTime()
                itemView.tvSymbolAlarmProfitabilityDay.text =
                    this.formatAlarmProfitability() + "/" + this.formatAlarmProfitabilityCumulative()
                itemView.tvSymbolAlarmDayLevelAndKNum.text =
                    getAlarmLevelAndKNum(item.prioritySetDay, item.priorityNumDay)

                itemView.tvSymbolAlarmLinkDaySelfRight.visibility = View.GONE

                if (LinkStatusConstant.isLowLinkHigh(this.extraSign)) {
                    if (LinkStatusConstant.isSelfLinkAssit(this.extraSign)) {
//                        itemView.tvSymbolAlarmLinkDaySelfRight.visibility = View.VISIBLE
                        itemView.tvSymbolAlarmLinkDaySelfRight.text =
                            ResourcesUtil.formatString(R.string.right_symbol)
                    }
                    if (!LinkStatusConstant.isLinkDayAssit(this.extraSign)) {
//                        itemView.tvSymbolAlarmLinkDaySelfRight.visibility = View.VISIBLE
                        itemView.tvSymbolAlarmLinkDaySelfRight.text = "x->"
                    }
                } else {
                    itemView.tvSymbolAlarmLinkDaySelfRight.text = " "
                }
                showTradingLineStatus(itemView.tvSymbolAlarmDayLevelLink, item.alarmDay)
                itemView.tvLinkStatusAndKLineNumDay.text =
                    this.getLinkStatusAndKLineNum(PeriodOffsetConstant.TYPE_MIN60)
                errorCodeText(itemView.tvSymbolBaseInfoDayCode, item.alarmDay)
            }
//            val expandVisibility = if (position == this.expendPosition) View.VISIBLE else View.GONE
//            itemView.expandDividerLine.visibility = expandVisibility
//            itemView.expandDayH12Container.visibility = expandVisibility
//            itemView.btnAlarmRecord.setOnClickListener {
//                AlarmRecordActivity.startActivity(context!!, item.currency + item.transCurrency)
//            }
            itemView.btnSymbolRecordH1.setOnClickListener {
                AlarmRecordActivity.startActivity(
                    context!!,
                    item.currency + item.transCurrency,
                    "60min"
                )
            }
            itemView.btnSymbolRecordH4.setOnClickListener {
                AlarmRecordActivity.startActivity(
                    context!!,
                    item.currency + item.transCurrency,
                    "4h"
                )
            }
            itemView.btnSymbolRecordH12.setOnClickListener {
                AlarmRecordActivity.startActivity(
                    context!!,
                    item.currency + item.transCurrency,
                    "12h"
                )
            }
            itemView.btnSymbolRecordDay.setOnClickListener {
                AlarmRecordActivity.startActivity(
                    context!!,
                    item.currency + item.transCurrency,
                    "1day"
                )
            }

        }

        private fun errorCodeText(text: TextView, info: AlarmInfo) {
            val errorCode = LinkStatusConstant.getStrategyErrorCode(info.extraSign)
            text.visibility = View.VISIBLE
            text.text = when {
                0 <= errorCode && errorCode < STRATEGY_ERR_TIPS.size -> STRATEGY_ERR_TIPS[errorCode]
                else -> "未知($errorCode)"
            }
        }

        /** 显示操盘线状态 */
        private fun showTradingLineStatus(textView: TextView, info: AlarmInfo) {
            val tradingLineCode = LinkStatusConstant.getTradingLineCode(info.extraSign)
            textView.visibility = View.VISIBLE
            textView.text = when {
                0 <= tradingLineCode && tradingLineCode < TRADING_LINE_TIPS.size -> TRADING_LINE_TIPS[tradingLineCode]
                else -> ""
            }
        }

        override fun onBindView(
            items: List<SymbolInfo>,
            item: SymbolInfo,
            position: Int,
            payloads: MutableList<Any>
        ) {
            (payloads[0] as? Boolean)?.let { twinkling ->
                item.alarmH1.visibleTwinkling(
                    context,
                    itemView.ivSymbolAlarmTwinklingH1,
                    twinkling && isAlarmTwinkling(item.alarmH1)
                )
                item.alarmH4.visibleTwinkling(
                    context,
                    itemView.ivSymbolAlarmTwinklingH4,
                    twinkling && isAlarmTwinkling(item.alarmH4)
                )
                item.alarmH12.visibleTwinkling(
                    context,
                    itemView.ivSymbolAlarmTwinklingH12,
                    expendPosition == position && twinkling && isAlarmTwinkling(item.alarmH12)
                )
//                item.alarmH12.visibleTwinkling(
//                    context,
//                    itemView.ivSymbolAlarmTwinklingTempH12,
//                    expendPosition != position && twinkling && isAlarmTwinkling(item.alarmH12)
//                )
                item.alarmDay.visibleTwinkling(
                    context,
                    itemView.ivSymbolAlarmTwinklingDay,
                    expendPosition == position && twinkling && isAlarmTwinkling(item.alarmDay)
                )
//                item.alarmDay.visibleTwinkling(
//                    context,
//                    itemView.ivSymbolAlarmTwinklingTempDay,
//                    expendPosition != position && twinkling && isAlarmTwinkling(item.alarmDay)
//                )
            }
        }

        private fun isAlarmTwinkling(alarmInfo: AlarmInfo): Boolean {
            return alarmInfo.isAlarmStatus()
        }


    }
}

private val decimalFormat = DecimalFormat("###################.###########")
private fun Double.ignoreEndZero() = decimalFormat.format(this)

/** 是否报警状态 */
private fun AlarmInfo.isAlarmStatus(): Boolean {
    return status == AlarmStatusConstant.VALUE_ALARM || status == AlarmStatusConstant.VALUE_ALARM_NEW
}

private fun AlarmInfo.formatPeriod(): String {
    return if (period < 0) {
        " "
    } else if (isAlarmStatus() || AlarmTypeConstant.isAlarmTypeStart(type) || AlarmTypeConstant.isAlarmTypeStand(
            type
        )
    ) {
        // 启动一直显示
        //大于等于10显示 >
        if (period >= 10) {
            ">"
        } else {
            this.period.toString()
        }
    } else if (AlarmTypeConstant.isAlarmTypeBreak(type)) {
        // 破线周期显示--
        "--"
    } else {
        // 失败或无状态时价格显示 " "
        " "
    }
}

private fun AlarmInfo.formatAlarmPrice(): String {
    return if (isAlarmStatus() || AlarmTypeConstant.isAlarmTypeStart(type) || AlarmTypeConstant.isAlarmTypeStand(
            type
        )
    ) {
        // 启动一直显示
        this.price.ignoreEndZero()
    } else if (AlarmTypeConstant.isAlarmTypeBreak(type)) {
        // 破线价格显示--
        "--"
    } else {
        // 失败或无状态时价格显示 " "
        " "
    }
}

private fun AlarmInfo.formatType(context: Context?): String {
    return if (AlarmTypeConstant.isAlarmTypeStart(this.type) || isAlarmStatus()) {
        // 报警类型汉字只显示15分钟后关闭 启除外  一直显示
        return AlarmTypeConstant.getAlarmTypeValue(type)
    } else {
        " "
    }
}

private fun AlarmInfo.formatAlarmTime(): String {
    return if (isAlarmStatus() || AlarmTypeConstant.isAlarmTypeStart(type) || AlarmTypeConstant.isAlarmTypeStand(
            type
        )
    ) {
        // 报警状态或启动一直显示
        DateFormat.format("dd-HH:mm", this.dateTime * 1000).toString()
    } else if (AlarmTypeConstant.isAlarmTypeBreak(type)) {
        // 破线时间位置显示sell
        "sell"
    } else if (AlarmTypeConstant.isAlarmTypeFailed(type)) {
        // 失败或无状态时时间位置显示 "fail"
        "fail"
    } else if (AlarmTypeConstant.isAlarmTypeEnd(type)) {
        // 周期结束有效卖出显示" "
        "end"
    } else {
        " "
    }
}

private fun AlarmInfo.formatAlarmProfitability(): String {
    return if (isAlarmStatus() || AlarmTypeConstant.isAlarmTypeStart(type) || AlarmTypeConstant.isAlarmTypeStand(
            type
        )
    ) {
        if (this.profitability > 100) {
            ResourcesUtil.formatString(R.string.fm_symbol_double_int, this.profitability)
        } else {
            ResourcesUtil.formatString(R.string.fm_symbol_double_one, this.profitability)
        }

    } else {
        " "
    }
}

private fun AlarmInfo.formatAlarmProfitabilityCumulative(): String {
    return if (isAlarmStatus() || AlarmTypeConstant.isAlarmTypeStart(type) || AlarmTypeConstant.isAlarmTypeStand(
            type
        )
    ) {
        if (this.profitabilityCumulative > 100) {
            ResourcesUtil.formatString(R.string.fm_symbol_double_int, this.profitabilityCumulative)
        } else {
            ResourcesUtil.formatString(R.string.fm_symbol_double_one, this.profitabilityCumulative)
        }

    } else {
        " "
    }
}


private fun AlarmInfo.getTwinklingColor(): String {
    if (AlarmTypeConstant.isAlarmTypeStart(type)) {
        return "#F08080" // 启动，淡红色
    } else if (AlarmTypeConstant.isAlarmTypeStand(type)) {
        return "#EEE685" // 站稳，淡黄色
    } else {
        return "#000000" // 破线、破位、卖出均用黑白色
    }
//    return when (type) {
//        1 -> "#F08080" // 启动，淡红色
//        3 -> "#EEE685" // 站稳，淡黄色
//        else -> "#000000" // 破线、破位、卖出均用黑白色
//    }
}

private fun AlarmInfo.getLinkStatusAndKLineNum(type: Int): String {
    var chain = PeriodOffsetConstant.comboLink4KLineChains(type, this.link4KlineChain).and(0x3)
    var linkStatus = LinkStatusConstant.getLink4KLineStatus(this.extraSign)
    var kNum: String
    if (this.link4KlineNum >= 100) {
        kNum = ">"
    } else {
        kNum = this.link4KlineNum.toString()
    }

    if (!AlarmTypeConstant.isBuyType(this.type)) {
        kNum = "0"
    }
    if (chain == 0L && linkStatus == 0) {
        return " " + kNum
    } else if (chain == 1L && linkStatus == 0) {
        return "-" + kNum
    } else {
        if (linkStatus == 1) {//当前K线实时满足
            return "★" + kNum
        } else if (linkStatus == 2) {//当前K线实时满足(满足过)
            return "☆" + kNum
        }
    }
    return " " + kNum
}

private fun AlarmInfo.visibleTwinkling(
    context: Context?,
    imageView: SVGImageView,
    twinkling: Boolean
) {
    if (twinkling) {
        val color = this.getTwinklingColor()
        val svg = BitmapUtil.getHornIconSVGByColor(context, color)
        if (svg != null) {
            imageView.setSVG(svg)
        }
        imageView.visibility = View.VISIBLE
    } else {
        imageView.visibility = View.INVISIBLE
    }
}

private fun AlarmInfo.getLowLinkHigh(): String {
    var rightTxt = ""
    if (!LinkStatusConstant.isLinkDayAssit(this.extraSign)) {
        rightTxt += "x"
    } else {
        rightTxt += "-"
    }
    if (LinkStatusConstant.isSelfLinkAssit(this.extraSign)) {
        rightTxt += "-x"
    } else {
        rightTxt += "->"
    }
    if (rightTxt.isNotEmpty()) {
        return rightTxt
    } else {
        return " "
    }
}

private fun AlarmInfo.getHighLinkLow(): String {
    var rightTxt = ""
    if (LinkStatusConstant.isControlLinkAssit(this.extraSign)) {
        rightTxt += "x"
    } else {
        rightTxt += "<"
    }
    if (!LinkStatusConstant.isControlLinkAssitMab(this.extraSign)) {
        rightTxt += "-x"
    } else {
        rightTxt += "--"
    }
    if (rightTxt.isNotEmpty()) {
        return rightTxt
    } else {
        return " "
    }
}

/**
 * 根据1：H1, 2：H4来获取不同的启，稳 颜色
 */
private fun getTextColorByStatus(alarmType: Int, alarmInfo: AlarmInfo): Int {
    //报警时间
    if (AlarmTypeConstant.isAlarmTypeStart(alarmInfo.type) || alarmInfo.status == AlarmStatusConstant.VALUE_ALARM || alarmInfo.status == AlarmStatusConstant.VALUE_ALARM_NEW) {
        //报警周期类型
        if (alarmType == 1) {
            if (AlarmTypeConstant.isAlarmTypeStart(alarmInfo.type)) {
                return Color.parseColor(PriceStatusColorEnum.ALARMH1_START.color) // 启动，淡红色
            } else if (AlarmTypeConstant.isAlarmTypeStand(alarmInfo.type)) {
                return Color.parseColor(PriceStatusColorEnum.ALARMH1_STAND.color) // 站稳，淡黄色
            } else {
                return Color.parseColor("#000000") // 破线、破位、卖出均用黑白色
            }
//            return when (alarmInfo.type) {
//                AlarmTypeConstant.VALUE_START -> Color.parseColor(PriceStatusColorEnum.ALARMH1_START.color) // 启动，淡红色
//                AlarmTypeConstant.VALUE_STAND -> Color.parseColor(PriceStatusColorEnum.ALARMH1_STAND.color) // 站稳，淡黄色
//                else -> Color.parseColor("#000000") // 破线、破位、卖出均用黑白色
//            }
        } else if (alarmType == 2) {
            if (AlarmTypeConstant.isAlarmTypeStart(alarmInfo.type)) {
                return Color.parseColor(PriceStatusColorEnum.ALARMH4_START.color) // 启动，淡灰色
            } else if (AlarmTypeConstant.isAlarmTypeStand(alarmInfo.type)) {
                return Color.parseColor(PriceStatusColorEnum.ALARMH4_STAND.color) // 站稳，淡蓝色
            } else {
                return Color.parseColor("#000000") // 破线、破位、卖出均用黑白色
            }
//            return when (alarmInfo.type) {
//                AlarmTypeConstant.VALUE_START -> Color.parseColor(PriceStatusColorEnum.ALARMH4_START.color) // 启动，淡灰色
//                AlarmTypeConstant.VALUE_STAND -> Color.parseColor(PriceStatusColorEnum.ALARMH4_STAND.color) // 站稳，淡蓝色
//                else -> Color.parseColor("#000000") // 破线、破位、卖出均用黑白色
//            }
        }
    }
    return Color.parseColor("#000000")//其他的都默认为黑白
}

private fun getAlarmLevelAndKNum(prioritySet: Long, priorityNum: Long): String {
    var level = PrioritySetConstant.getSetLevelInDiffType(prioritySet)
    if (level == 999) {
        return " "
    }
    var kNum = PriorityNumConstant.getPriorityNumByLevel(level, priorityNum)
    var result = ""
    if (level >= 10) {
        result += ">"
    } else {
        result += level
    }
    if (PrioritySetConstant.isPrioritySetDanger(prioritySet)) {
        result += "-"
    } else {
        result += "_"
    }

    if (kNum >= 10) {
        result += ">"
    } else {
        result += kNum
    }
    return result
}